package cn.itcast.usermanage.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;

import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;

@Controller
@RequestMapping("rest/user") //请求路径不要使用大写字母
public class RestfulUserController {
	
	@Autowired
	private UserService userService;
	
/*	@RequestMapping(value="{id}", method=RequestMethod.GET)
	@ResponseBody
	public User queryUserById(@PathVariable("id")Long id) {
		return this.userService.queryUserById(id);
	}*/
	
	/**
	 * 根据id查询用户：GET
	 * @param id
	 * @return
	 */
	@RequestMapping(value="{id}", method=RequestMethod.GET)
//	@ResponseBody //使用了REST风格的ResponseEntity<User>，不再需要@ResponseBody这个注解
	public ResponseEntity<User> queryUserById(@PathVariable("id")Long id) {
		try {
			if (id == null || id.longValue() < 0) {
				// BAD_REQUEST:400，参数列表错误
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
			}
//			int i = 1/0;
			User user = this.userService.queryUserById(id);
			
			if (user == null) {
				// NOT_FOUND：404，资源未找到
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(user);
			}
//		return ResponseEntity.status(HttpStatus.OK).body(user);
			return ResponseEntity.ok(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// INTERNAL_SERVER_ERROR:500，服务器内部错误，代码有BUG
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	/**
	 * 新增用户：POST
	 * @param user
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST)
	public ResponseEntity<Void> addUser(User user) {
		try {
			if (user == null || StringUtils.isEmpty(user.getUserName())) {
				// 泛型是Void时，使用build方法；泛型不是Void时，使用body方法
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();		
			}
			Boolean b = this.userService.addUser(user);
			if (b) {
				// CREATED:201,资源创建成功
				return ResponseEntity.status(HttpStatus.CREATED).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	/**
	 * 更新（修改）用户：PUT
	 * @param user
	 * @return
	 */
	@RequestMapping(method=RequestMethod.PUT)
	public ResponseEntity<Void> updateUser(User user) {
		try {
			if (user == null || user.getId() == null || user.getId().longValue() <= 0) {
				// 泛型是Void时，使用build方法；泛型不是Void时，使用body方法
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();		
			}
			Boolean b = this.userService.updateUser(user);
			if (b) {
				// NO_CONTENT:204,执行已成功，但没有返回数据
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	/**
	 * 删除用户：DELETE
	 * @param user
	 * @return
	 */
	@RequestMapping(method=RequestMethod.DELETE)
	public ResponseEntity<Void> deleteUser(@RequestParam("ids")List<Object> ids) {
		try {
			if (ids == null || ids.size() == 0) {
				// 泛型是Void时，使用build方法；泛型不是Void时，使用body方法
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();		
			}
			Boolean b = this.userService.deleteUser(ids);
			if (b) {
				// NO_CONTENT:204,执行已成功，但没有返回数据
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
}
